package se.webbzon.boltzmann.minigame.page;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;

import se.webbzon.oschi01.font.HorizontalAlignment;
import se.webbzon.oschi01.font.VerticalAlignment;
import se.webbzon.oschi01.utilities.StringUtils;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;

public class PageText implements PageItem {
	
	// The layout of this info text item
	private final PageLayout layout;
	
	// The text lines of this info text item
	private final List<String> lines;
	
	// The header level of the text
	private int level;
	
	// Whatever this text is bold and/or italic
	private boolean bold, italic;
	
	// The color of the text
	private Color color;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public PageText(PageLayout layout, String text) {
		this.layout = layout;
		lines = new ArrayList<String>();
		String[] paragraphs = text.split("\n");
		for (int i = 0; i < paragraphs.length; i++) {
			String paragraph = StringUtils.divide(paragraphs[i], layout.getLineMaxLength());
			String[] innerLines = paragraph.split("\n");
			for (int j = 0; j < innerLines.length; j++)
				lines.add(innerLines[j]);
			
			if (i < paragraphs.length-1) // Don't add extra line on last paragraph
				lines.add("");
		}
		bold = false;
		italic = false;
		level = 0;
		color = Color.black;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the color of the text item. **/
	public Color getColor() {
		return color;
	}
	
	/** Sets the color of the text item. **/
	public void setColor(Color color) {
		this.color = color;
	}
	
	/** Returns the header level of this text item. **/
	public int getLevel() {
		return level;
	}
	
	/** Sets the header level of this text item. **/
	public void setLevel(int level) {
		this.level = level;
	}
	
	/** Returns true if this info text item is bold. **/
	public boolean isBold() {
		return bold;
	}

	/** Sets whatever this info text item is bold. **/
	public void setBold(boolean bold) {
		this.bold = bold;
	}

	/** Returns true if this info text item is italic. **/
	public boolean isItalic() {
		return italic;
	}

	/** Sets whatever this info text item is italic. **/
	public void setItalic(boolean italic) {
		this.italic = italic;
	}

	@Override public void draw(WorldCameraPane hud, int x, int y) {
		final Font font = layout.getFont(level, bold, italic);
		final int dy = font.getSize() + layout.getLineSpacing();
		hud.setFont(font);
		hud.setColor(color);
		hud.setHAlignment(HorizontalAlignment.LEFT);
		hud.setVAlignment(VerticalAlignment.TOP);
		for (String line : lines) {
			hud.drawString(line, x+4, y);
			y += dy;
		}
	}

	@Override public int getHeight() {
		final Font font = layout.getFont(level, bold, italic);
		final int dy = font.getSize() + layout.getLineSpacing();
		return dy * lines.size();
	}
	
	@Override public void onStep(Page page) {};
	
	/*============================================================
	Protected Methods
	============================================================*/
	
	/** Returns the layout of this page text item. **/
	public PageLayout getLayout() {
		return layout;
	}

}
